Passed
Pull Request — master (#66)
by Michael
02:50
created

SetDocAttrStep.js ➔ constructor   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 5
nc 1
nop 3
dl 0
loc 6
rs 10
c 1
b 0
f 0
1
import { Step, StepResult } from 'prosemirror-transform';
2
3
/**
4
 * Custom step to set attributes of the root `doc` node
5
 *
6
 * @see https://discuss.prosemirror.net/t/changing-doc-attrs/784/17
7
 */
8
export default class SetDocAttrStep extends Step {
9
    constructor(key, value, stepType = 'SetDocAttr') {
10
        super();
11
        this.stepType = stepType;
12
        this.key = key;
13
        this.value = value;
14
    }
15
16
    apply(doc) {
17
        this.prevValue = doc.attrs[this.key];
18
        doc.attrs[this.key] = this.value;
19
        return StepResult.ok(doc);
20
    }
21
22
    invert() {
23
        return new SetDocAttrStep(this.key, this.prevValue, 'revertSetDocAttr');
24
    }
25
26
    map() {
27
        return null;
28
    }
29
30
    toJSON() {
31
        return {
32
            stepType: this.stepType,
33
            key: this.key,
34
            value: this.value,
35
        };
36
    }
37
38
    static fromJSON(json) {
39
        return new SetDocAttrStep(json.key, json.value, json.stepType);
40
    }
41
}
42